import java.util.HashMap;

public class code01 {
    //给定一个整数数组nums和一个整数目标值，请你在该数组中找出和为目标值的两个整数，并返回他们的下标
//    输入：nums = [2,7,11,15], target = 9nums = [3,2,4], target = 6
////    输出：[0,1]
    public static void main(String[] args) {
        int[] ints = {3,3,4};
        int[] ints1 = twoSummap(ints, 6);
        for (int i : ints1) {
            System.out.println(i);
        }
    }
        //耗费时间68ms
        //暴力解法
      public   static int[] twoSum(int[] nums, int target) {
          for (int i = 0; i < nums.length-1; i++) {
              for (int j = i+1; j <nums.length ; j++) {
                  if (nums[i]+nums[j]==target){
                      return  new int[]{i,j};
                  }
              }
          }

          return nums;
      }
        //耗费时间4ms
        //用map实现  key存储数组，value存储数组的下标
        public   static int[] twoSummap(int[] nums, int target) {
            int[] ints = new int[2];
            HashMap<Integer,Integer> hashMap = new HashMap();
            for (int i = 0; i < nums.length; i++) {

                int val=target-nums[i];
                if (hashMap.containsKey(val))  {
                    ints[0]=hashMap.get(val);
                    ints[1]=i;
                }
                hashMap.put(nums[i],i);
            }

            return ints;
        }
}

